var ws = require("nodejs-websocket");
var server = ws.createServer(function(conn){
    conn.on("text", function (str) {
        console.log("收到的信息为:"+str)
        var backMsg = callBack(str);
        console.log("转换的信息为:"+backMsg)
        conn.sendText(JSON.stringify(backMsg))
    })
    conn.on("close", function (code, reason) {
        console.log("关闭连接")
    });
    conn.on("error", function (code, reason) {
        console.log("异常关闭")
    });
}).listen(3001)
var msgJson = {
    'QUERYUSERLOGIN':{
        "body" : {
           "loginSuccess" : true,
           "serverMac" : "f0-b4-29-31-56-c2",
           "userAuthority" : {
              "deviceConfiguration" : true,
              "deviceManagement" : true,
              "event" : true,
              "eventConfiguration" : true,
              "exhibition" : true,
              "log" : true,
              "mediaManagement" : true,
              "messageWidgets" : true,
              "mission" : true,
              "preview" : true,
              "program" : true,
              "record" : true,
              "replay" : true,
              "systemConfiguration" : true,
              "userManagement" : true,
              "videoWallManagement" : true
           }
        },
        "category" : 0,
        "errorCode" : 0,
        "errorStr" : "OK",
        "guid" : "M-0",
        "type" : "QUERYUSERLOGIN"
     },
     'ADDWIDGETC':{
        "body" : {
           "clock" : {
              "countdowndatatime" : "00:10:00",
              "iscountdownclock" : false,
              "language" : true,
              "showAnalogClock" : false,
              "showDate" : true,
              "showDigitalClock" : true,
              "showIn24Hours" : false,
              "showbackground" : false
           },
           "height" : 400,
           "id" : 306,
           "slaveID" : 100,
           "slave_scale_height" : 0.370370,
           "slave_scale_width" : 0.1250,
           "slave_scale_x" : 0.0,
           "slave_scale_y" : 0.0,
           "type" : 0,
           "widgetPositionXML" : "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<Root>\n    <X>0</X>\n    <Y>0</Y>\n    <Width>400</Width>\n    <Height>400</Height>\n    <Slave ID=\"100\">\n        <X>0</X>\n        <Y>0</Y>\n        <Width>0.125</Width>\n        <Height>0.37037</Height>\n    </Slave>\n</Root>\n",
           "width" : 400,
           "x" : 0,
           "y" : 0,
           "zorder" : 1501
        },
        "category" : 0,
        "errorCode" : 0,
        "errorStr" : "OK",
        "guid" : "M-117",
        "type" : "ADDWIDGET"
     },
     'ADDWIDGETW':{
        "body" : {
           "height" : 400,
           "id" : 307,
           "slaveID" : 100,
           "slave_scale_height" : 0.370370,
           "slave_scale_width" : 0.1250,
           "slave_scale_x" : 0.0,
           "slave_scale_y" : 0.0,
           "type" : 1,
           "weather" : {
              "city" : "Shanghai",
              "highTemperature" : "20",
              "language" : "zh-CN",
              "lowTemperature" : "10",
              "proxyIP" : "",
              "proxyPort" : "",
              "temperFormat" : "C",
              "useProxy" : false,
              "useTrans" : false,
              "weatherImage" : "32d.png",
              "weatherName" : "Weather",
              "weatherTitle" : "tornado",
              "woeid" : "2151849"
           },
           "widgetPositionXML" : "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n<Root>\n    <X>0</X>\n    <Y>0</Y>\n    <Width>400</Width>\n    <Height>400</Height>\n    <Slave ID=\"100\">\n        <X>0</X>\n        <Y>0</Y>\n        <Width>0.125</Width>\n        <Height>0.37037</Height>\n    </Slave>\n</Root>\n",
           "width" : 400,
           "x" : 0,
           "y" : 0,
           "zorder" : 1501
        },
        "category" : 0,
        "errorCode" : 0,
        "errorStr" : "OK",
        "guid" : "M-31",
        "type" : "ADDWIDGET"
     }
}

function callBack(str){
    var obj = JSON.parse(str);
    var backMsg = str;
	if(!obj){
		return backMsg;
    }else{
        if(obj.type == 'QUERYUSERLOGIN'){
            backMsg = msgJson.QUERYUSERLOGIN
        }else if(obj.type == 'ADDWIDGET'){
            if(obj.body.type=='1'){
                backMsg = msgJson.ADDWIDGETW
            }else{
                backMsg = msgJson.ADDWIDGETC
            }
        }
        return backMsg;
    }
}
console.log("--WebSocket-------------")
console.log("WebSocket address: ws://127.0.0.1:3001")
console.log("WebSocket has started.")
console.log("------------------------")


var http = require("http"),
    url  = require("url"),
    path = require("path"),
    fs   = require("fs");

http.createServer(function (req, res) {
    var pathname=__dirname+url.parse(req.url).pathname;
    if (path.extname(pathname)=="") {
        pathname+="/";
    }
    if (pathname.charAt(pathname.length-1)=="/"){
        pathname+="test/index.html";
    }

    fs.stat(pathname,function(err){
        if (!err){
            switch(path.extname(pathname)){
                case ".html":
                    res.writeHead(200, {"Content-Type": "text/html"});
                    break;
                case ".js":
                    res.writeHead(200, {"Content-Type": "text/javascript"});
                    break;
                case ".css":
                    res.writeHead(200, {"Content-Type": "text/css"});
                    break;
                case ".gif":
                    res.writeHead(200, {"Content-Type": "image/gif"});
                    break;
                case ".jpg":
                    res.writeHead(200, {"Content-Type": "image/jpeg"});
                    break;
                case ".png":
                    res.writeHead(200, {"Content-Type": "image/png"});
                    break;
                default:
                    res.writeHead(200, {"Content-Type": "application/octet-stream"});
            }

            fs.readFile(pathname,function (err,data){
                res.end(data);
            });
        } else {
            res.writeHead(404, {"Content-Type": "text/html"});
            res.end("<h1>404 Not Found</h1>");
        }
    });

}).listen(8090, "127.0.0.1");


console.log("--Server----------------")
console.log("Server address: http://127.0.0.1:8090")
console.log("Server running... press ctrl-c to stop.")
console.log("Server has started.")
console.log("------------------------")